package com.gitzzp.materialdesignsample;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Property;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

/**
 * 创建人：gitzzp
 * 创建日期:17/2/14 15:57
 * 类描述:
 */
public class MyView extends View {

    //默认画笔
    private Paint defaultPaint = new Paint();

    //文字画笔
    private Paint paint = new Paint();

    //文字path
    private Path path = null;

    //文字字号
    private int textSize = 35;

    //文字宽度
    private int textWidth;

    //文字高度
    private int textHeight;

    //文字开始宽度
    private int textStartWidth = 200;

    //文字开始高度
    private int textStartHeight = 400;

    //文字
    private String text = "哎呦不错哦,哎呦不错哦";

    //初始化画笔并设置
    {
        path = new Path();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(3);
        //画笔设置镂空 否则曲线不闭合也会被填充
        paint.setStyle(Paint.Style.STROKE);

        paint.setTextSize(textSize);
    }

    //文字跳动的偏移量
    private float textOffset = 0;

    //图片上下动画的偏移量
    private float bitmapOffset = 0;

    //文字跳动的属性动画
    ObjectAnimator offsetAnimator = null;

    Property<MyView,Float> myViewFloatProperty = new Property<MyView, Float>(Float.class,"offset") {
        @Override
        public Float get(MyView myView) {
            return myView.getTextOffset();
        }

        @Override
        public void set(MyView object, Float value) {
            object.setTextOffset(value);
        }
    };

    //图片上下的两个动画
    ValueAnimator downAnimator = null;
    ValueAnimator upAnimator = null;

    //图片
    Bitmap bitmap;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){

        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);

        initAnimator();
    }

    /**
     * 初始化三个动画
     */
    private void initAnimator(){
        offsetAnimator = ObjectAnimator.ofFloat(this,myViewFloatProperty,0,50,0);
        offsetAnimator.setDuration(300);
//        offsetAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        offsetAnimator.setInterpolator(new BounceInterpolator());

        offsetAnimator.addListener(new AnimatorListenerImpl(){
            @Override
            public void onAnimationEnd(Animator animator) {
                super.onAnimationEnd(animator);
            }
        });



        downAnimator = ValueAnimator.ofInt(0,textStartHeight-bitmap.getHeight()-textHeight-10);

        downAnimator.setDuration(1000);

        downAnimator.setInterpolator(new LinearInterpolator());

        downAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            private IntEvaluator intEvaluator = new IntEvaluator();
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int currentValue = (int) valueAnimator.getAnimatedValue();

                setBitmapOffset(currentValue);
//                float fraction = valueAnimator.getAnimatedFraction();
//
//                setBitmapOffset(intEvaluator.evaluate(fraction, (int) getBitmapOffset(),200));


            }
        });

        downAnimator.addListener(new AnimatorListenerImpl(){
            @Override
            public void onAnimationEnd(Animator animator) {
                super.onAnimationEnd(animator);
                offsetAnimator.start();
                upAnimator.start();
            }
        });

        upAnimator = ValueAnimator.ofInt(textStartHeight-bitmap.getHeight()-textHeight-10,0);
        upAnimator.setDuration(1000);
        upAnimator.setInterpolator(new DecelerateInterpolator());
        upAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                setBitmapOffset((int)valueAnimator.getAnimatedValue());
            }
        });

        upAnimator.addListener(new AnimatorListenerImpl(){
            @Override
            public void onAnimationEnd(Animator animator) {
                super.onAnimationEnd(animator);
                downAnimator.start();
            }
        });

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        downAnimator.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        textHeight = (int) (paint.getFontMetrics().bottom-paint.getFontMetrics().top);
        //测量文字的宽度
        textWidth = measureTextWidth(paint,text);

        Log.d("MyView", "textWidth:" + textWidth);
        Log.d("MyView", "textHeight:" + textHeight);

    }

    //使用画笔来测量文字的具体宽度
    private int measureTextWidth(Paint paint,String text){
        int width = 0;
        if(text!=null&&text.length()>0){
            float[] widths = new float[text.length()];
            paint.getTextWidths(text,widths);
            for(int i = 0;i<widths.length;i++){
                width+=widths[i];
            }
            return width;
        }
        return 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(bitmap,textStartWidth+textWidth/2-bitmap.getWidth()/2,getBitmapOffset(),defaultPaint);

        path.reset();
        path.moveTo(textStartWidth,textStartHeight);
        path.quadTo(textStartWidth+textWidth/2,textStartHeight+getTextOffset(),textStartWidth+textWidth,textStartHeight);
//        path.cubicTo(300,300,400,100,600,200);
        canvas.drawPath(path,paint);
//        path.close();
        canvas.drawTextOnPath(text,path,0,0,paint);

    }


    public float getTextOffset() {
        return textOffset;
    }

    public void setTextOffset(float textOffset) {
        this.textOffset = textOffset;
        invalidate();
    }

    public float getBitmapOffset() {
        return bitmapOffset;
    }

    public void setBitmapOffset(float bitmapOffset) {
        this.bitmapOffset = bitmapOffset;
        invalidate();
    }


    class AnimatorListenerImpl implements Animator.AnimatorListener{

        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {

        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    }
}
